package com.ydzt.fanka.server.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/*
 * XML帮助类 
 * 对报文定义文件的操作
 * */
public class XmlUtil {
	public static List<Element> GetOneRecordDefine(String pkgPath){
		Document doc = XmlUtil.ReadXml(pkgPath);
		List<Element> list = XmlUtil.GetNodes(doc);
		return list;
	}
	

	/*
	 * 读取xml文件为Document对象
	 * */
	public static Document ReadXml(String strPathName)
	{
		try {
			Document doc = null;
			BufferedReader reader = new BufferedReader(new FileReader(strPathName));
			String tempStr;
			StringBuffer ewXml = new StringBuffer();
			while ((tempStr = reader.readLine()) != null)
			{
				ewXml.append(tempStr);
			}
			reader.close();
			doc= DocumentHelper.parseText(ewXml.toString());
			if(doc == null)
			{
				System.out.println("xml文件歇息不成功");
			}
			return doc;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String GetTableOrder(Document doc){
		Element root = doc.getRootElement();
		String order = root.attributeValue("ORDER");
		return order;
	}
	
	/*
	 * 从Document对象中取xml的各个节点
	 * */
	public static List<Element> GetNodes(Document doc)
	{
		if(doc != null)
		{
			List<Element> list = new ArrayList<Element>();
			Element root = doc.getRootElement();
			List<?> nodes = root.elements();
			for(Iterator<?> it = nodes.iterator(); it.hasNext();)
			{
				Element elm = (Element)it.next();
				//System.out.println(elm.getName());
				list.add(elm);
			}
			return list;
		}
		else
		{
			return null;
		}
	}

	public static int GetRootNodeAttributeLength(String path){
		Document doc = ReadXml(path);
		Element root = doc.getRootElement();
		int length = Integer.parseInt(root.attributeValue("Length"));
		return length;
	}
	
	/*
	 * 取根节点的长度 也就是所定义报文的包体长度
	 * */
	public static int getRootNodeAttributeLength(Document doc){
		Element root = doc.getRootElement();
		int length = Integer.parseInt(root.attributeValue("Length"));
		return length;
	}
	
	/*
	 * 取根节点的Count也就是所定义报文中信息节点数
	 * */
	public static int getRootNodeAttributeCount(String path){
		Document doc = ReadXml(path);
		Element root = doc.getRootElement();
		int count = Integer.parseInt(root.attributeValue("Count"));
		return count;
	}
	
	/*
	 * 取根节点的Count也就是所定义报文中信息节点数
	 * */
	public static int getRootNodeAttributeCount(Document doc){
		Element root = doc.getRootElement();
		int count = Integer.parseInt(root.attributeValue("Count"));
		return count;
	}
	
	/*
	 * 取子节点的Type 也就是解包拼接sql时表中对应的字段名
	 * */
	@SuppressWarnings("unused")
	private String GetNodeTypeByNode(Element elm){
		return elm.attributeValue("Type");
	}
	
	/*
	 * 取子节点的长度 也就是解析该段报文时要解析的长度
	 * */
	@SuppressWarnings("unused")
	private String GetNodeLengthByNode(Element elm){
		return elm.attributeValue("Length");
	}
	
	/*
	 * 取子节点的Name 也就是解包拼接sql时表中对应的字段名
	 * */
	private static String GetNodeNameByNode(Element elm){
		return elm.attributeValue("Name");
	}
	
	public static List<String> getNodesNameCollection(String path){
		Document doc = ReadXml(path);
		List<Element> nodes = GetNodes(doc);
		List<String> nameList = new ArrayList<String>();
		int count = nodes.size();
		for(int i = 0; i < count;i ++){
			Element elm = nodes.get(i);
			String name = GetNodeNameByNode(elm);
			nameList.add(name);
		}
		return nameList;
	}

	/*
	 * 取根节点名称 即此报文解析时对应的表名
	 * */
	public static String GetRootName(Document doc){
		return doc.getRootElement().getName();
	}
}